
cmp.ready(function () {
	
	var urlParam = cmp.href.getParam() || {};
	
	var app = new Vue({
		el: '#app',
		data: {
			style:{
				seeDetailIndex: -1,
				hasMore: true
			},
			basic:{
				mark: urlParam.getdataMark
			},
			paginationData:{
				page: 1,
				per_page_size: 20,
				total_row_size: 0
			},
			config: null,
			queryForm:{
				code: null,
				name: null
			},
			queryParams: [{
				key: 'name',
				name: '名称',
				inKey: null,
				type: 'string',
				defaultValue: null
			},{
				key: 'code',
				name: '编码',
				inKey: null,
				type: 'string',
				defaultValue: null
			}],
			queryFParams:[],
			dataTitle:[{
				key: 'name',
				name: '姓名',
				width: 300,
				display: true,
				lock: false,
				example: '...'
			},{
				key: 'code',
				name: '编码',
				width: 300,
				display: true,
				lock: false,
				example: '...'
			}],
			dataList:[{
				name: '...',
				code: '...'
			},{
				name: '...',
				code: '...'
			}]
		},
		methods:{
			init: function(){
				
				this.loadConifg();
				
			},
			loadConifg: function(){

				var _this = this;
				
				Snail.http.get({
					url: (cmp.seeyonbasepath + '/snail/formext/getdata.do?method=getConfig'),
					header: {'Accept': 'application/json'},
					data:{mark: this.basic.mark},
					onsuccess: function(result, status, xhr){
						
						var data = null;

						if(200 == result.code){
							
							data = result.data;
							
						}else{
							cmp.dialog.error(result.message);
						}
						
						if(null != data){

							_this.config = data;
														
							(function(config){
								
								var queryForm = {};
								var queryParams = [];
								var queryFParams = [];
								
								for(var i=0,len=config.length; i<len; i++){
									
									queryForm[config[i].key] = null;
									
									if(Snail.isBlank(config[i].inKey)){
									
										queryParams.push(config[i]);
									}else{
										queryFParams.push(config[i]);
									}
								}
								
								_this.queryForm = queryForm;
								_this.queryParams = queryParams;
								_this.queryFParams = queryFParams;
							})(data.dataset['in']);
							
							_this.dataTitle = (function(config){
								
								var size = 0;
								
								for(var i=0,len=config.length; i<len; i++){
									
									if(true == config[i].lock){
										size++;
									}
								}
								
								if(size < 2){ // 小于2列锁定时，补全
									
									size = 2 - size;
									
									for(var i=0,len=config.length; i<len; i++){
										
										if(true != config[i].lock && true == config[i].display){
											
											config[i].lock = true;
											
											size--;
											
											if(0 >= size){
												break;
											}
										}
									}
								}

								return config;
							})(data.dataset['out']);
							
							_this.execQuery();
							
						}else{
							cmp.dialog.error('获取配置信息为空！');
						}
						
					},
					onerror: function(responseText, status, xhr){
						
						console.log(responseText);

						cmp.dialog.error('HTTP STATUS: ' + status + ', MESSAGE: ' + responseText);
						
					}
				});
			},
			execQuery: function(isAppend){
				
				var _this = this;
				
				Snail.http.post({
					url: cmp.seeyonbasepath + '/snail/formext/getdata.do?method=getData&mark=' + this.basic.mark,
					header: {
						'Content-Type': 'application/json', 'Accept': 'application/json',
						'pagination.page': this.paginationData.page, 
						'pagination.per_page_size': this.paginationData.per_page_size
					},
					data: (function(data, config, config2, formData, formmains, formsons, recordId){
						
						var key = null;
						var value = null;
						var param = {};
						
						for(var i=0,len=config.length; i<len; i++){
							
							key = config[i].key;
							value = data[key];
							
							if('like' == config[i].type){
								param[key] = ('%' + (!Snail.isBlank(value) ? value : (config[i].defaultValue || '')) + '%');
							}else{
								param[key] = (!Snail.isBlank(value) ? value : config[i].defaultValue);
							}
						}
						
						for(var i=0,len=config2.length; i<len; i++){
							
							key = config2[i].key;
							
							value = (function(key, type, data, formmains, formsons, recordId){

							    var tableName = key.substring(0, key.indexOf('.'));
							    
								key = key.substring(key.indexOf('.') + 1);
								
								if(formmains[tableName]){
									
									var fieldInfo = formmains[tableName].elements[key];
									
									if(fieldInfo){
										return fieldInfo.value;
									}
								}

								if(recordId){

									if(formsons[tableName]){
										
										for(var i=0,len=formsons[tableName].records.length; i<len; i++){

											if(!formsons[tableName].records[i]){
												continue;
											}
											
											if(recordId == formsons[tableName].records[i].id){
												
												var fieldInfo = formsons[tableName].records[i].elements[key];
												
												if(fieldInfo){
													return fieldInfo.value;
												}
											}
										}
									}
								}
								
								return ('like' == type ? '%%' : null);
								
							})(config2[i].inKey, config2[i].type, formData, formmains, formsons, recordId);
							
							if('like' == config2[i].type){
								param[key] = ('%' + (!Snail.isBlank(value) ? value : (config2[i].defaultValue || '')) + '%');
							}else{
								param[key] = (!Snail.isBlank(value) ? value : config2[i].defaultValue);
							}
						}
												
						return param;
					})(this.queryForm, this.queryParams, this.queryFParams, urlParam.data, urlParam.formmains, urlParam.formsons, urlParam.recordId),
					onsuccess: function(result, status, xhr){
						
						if(200 == result.code){
							
							if(!Snail.isBlank(result.data) && Snail.isArray(result.data)){
								
								var unqiue = [];
								var out = _this.config.dataset.out;
								
								for(var i=0,len=out.length; i<len; i++){
									
									if(out[i]){
										unqiue.push(out[i].key);
									}
								}
								
								var generateUnqiue = null;
								
								if(0 == unqiue.length){
									
									generateUnqiue = function(row, index){

										if(null == row){
											return;
										}
										
										row['__SNAIL_UNQIUE_KEY__'] = new String(((_this.paginationData.page - 1) * _this.paginationData.per_page_size) + index);
									}
									
								}else{
									
									generateUnqiue = function(row, index){
										
										if(null == row){
											return;
										}
										
										var strUnqiue = '';
										
										for(var i=0,len=unqiue.length; i<len; i++){
											
											strUnqiue += row[unqiue[i]];
										}
										
										row['__SNAIL_UNQIUE_KEY__'] = strUnqiue;
									}
								}
								
								var newData = [];
								
								var len = result.data.length;
								
								if(len > _this.paginationData.per_page_size){
									
									len = _this.paginationData.per_page_size;
									
									console.log('GetDATA返回的数据长度超过分页值（' + _this.paginationData.per_page_size + '），实际长度为' + result.data.length);
								}
								
								for(var i=0; i<len; i++){
									
									generateUnqiue(result.data[i], i);
									
									newData.push(result.data[i]);
								}
								
								if(isAppend){
									
									_this.dataList = _this.dataList.concat(newData);
									
								}else{
									_this.dataList = newData;
								}
								
							}else{
								
								if(!isAppend){
									_this.dataList = null;
								}
							}
							
						} else {
							
							if(!isAppend){
								_this.dataList = null;
							}
							
							cmp.dialog.error(result.message);
						}
						
					},
					onerror: function(responseText, status, xhr){
						
						if(!isAppend){
							_this.dataList = null;
						}
						
						console.log(responseText);

						cmp.dialog.error('HTTP STATUS: ' + status + ', MESSAGE: ' + responseText);
						
					},
					oncomplete: function(responseText, status, xhr){
						
						var totalRowSize = xhr.getResponseHeader('pagination.total_row_size');
						var totalPageSize = xhr.getResponseHeader('pagination.total_page_size');
						var page = xhr.getResponseHeader('pagination.page');

						_this.paginationData.total_row_size = totalRowSize ? window.parseInt(totalRowSize) : 0;
						
						_this.style.hasMore = totalPageSize > page;
					}
				});
			},
			onQuery: function(){
				
				this.execQuery();
			},
			onReset: function(){

				this.queryForm = (function(data){
					
					var newData = {};
					
					for(var name in data){
						newData[name] = null;
					}
					
					return newData;
				})(this.queryForm);

				this.execQuery();
			},
			onSelectSingleData: function(index){
				
				var data = (function(dataList, index){
					
					if(!dataList || index >= dataList.length){
						return null;
					}
					
					var newData = {};
					
					if(dataList[index]){
						
						for(n in dataList[index]){
							
							newData[n] = dataList[index][n];
						}
					}
					
					return newData;
					
				})(this.dataList, index);
				
				if(data){

					cmp.storage.save(urlParam.backfillDataCacheMark, cmp.toJSON({
						data: [data],
						config: this.config
					}), true);
					
					if(cmp.platform.CMPShell){
						
						// 触发 M3 webview事件
						cmp.webViewListener.fire({
							type : "WEBVIEW_EVENT_GETDATA_BACKFILLDATA",
							data : {
								data: [data],
								config: this.config
							},
							success:function(){
								cmp.href.closePage();
							},
							error:function(error){
								console.log(error);
							}
						});
						
					}else{
						
						cmp.href.back();
					}
						
				}else{
					cmp.href.back();
				}
			},
			onSeeDetail: function(index){
				
				this.style.seeDetailIndex = index;
				
			},
			onPaginationSizeChange: function(val){
				
				this.paginationData.per_page_rows = val;
				
				this.execQuery();
			},
			onLoadMore: function(){
				
				this.paginationData.page++;
				
				this.execQuery(true);
			},
			onClose: function(){
				;
			}
			
		},
		created: function(){
			this.init();
		},
		watch:{
			
		}
	});
	
	
});
